<?php
/**
 * Created by PhpStorm.
 * User: Kiriti
 * Date: 2016/3/29 0029
 * Time: 17:15
 */


namespace app\controllers;
use yii\web\Controller;
use Yii;
use app\models\Uc_user;
use app\models\Pr_like;
use app\models\Pr_project;
use app\models\Rs_raised;
use app\models\Pr_support;
use app\models\Uc_msg;
use app\models\Uc_area;
use app\models\Pr_field;
use app\controllers\PrlikeController;
use yii\helpers;

define('HOST', 'http://localhost/chuangwu/web/index.php?r=');
class UsercenterController extends Controller
{
//    public $layout=false;
    public function actionShow(){
        
        if(!empty($_COOKIE['user_id'])){
                $user_id = $_COOKIE['user_id'];
                $user = $this->getUserInfo($user_id);
                $provinces = $this->getProvince();
                $fields = $this->getFields();

                $view = Yii::$app->view;
                $view->params['title'] = "个人资料";
                PrlikeController::setView($user);

                return $this->render('usercenter', ['user'=>$user, 'provinces'=>$provinces, 'fields'=>$fields]);
        }else{
            echo "<script>alert('您还未登录 请先登录')</script>";
            echo "<script>location.href='http://localhost/chuangwu/web/index.php?r=login/show'</script>";
        }
        
    }
    public function getFields(){
        $query = Pr_field::find()
            ->all();
        return $query;
    }
    public function getProvince($father_id = -1){
        $query = Uc_area::find()
            ->where([
                    'father_id' => $father_id,
                    'del_flag' => 0
                ])->all();
        return $query;
    }
    public function actionGetprovince($father_id = -1){
        $query = Uc_area::find()
            ->where([
                'father_id' => $father_id,
                'del_flag' => 0
            ])->all();
        echo helpers\Json::encode($query);


//        echo mb_convert_encoding(json_encode($query[0]['province']),'UTF-8');
    }
    public function transferJson($arrays){
        $return_string = "{";
        foreach($arrays as $array){

        }
    }

    public function getUserInfo($user_id){

        $query = Uc_user::find()
            ->where([
                'user_id' => $user_id, //user_id => user_id
                'del_flag' => 0
            ])
            ->one();
        return $query;

    }

    public function getProjectLike($user_id){  //problems here

        $query = Pr_like::find()
            ->where([
                'user_id' => $user_id, //user_id => user_id
                'del_flag' => 0
            ])
            ->all();
        $inList = "";
        foreach($query as $key=>$id){
            $inList += $id['project_id'];
            $inList += ",";
        }
        echo $inList;
        if($inList === ""){
            $inList += "-1";
        }else{
            $inList = substr($inList, 0, -1);
        }
        $projects = Pr_project::findBySql('SELECT * FROM pr_project WHERE pr_id in ('.$inList.')')->all();
        return $projects;

    }

    public function getProjectLaunch($user_id){

        $query = Pr_project::find()
            ->where([
                'user_id' => $user_id, //user_id => user_id
                'del_flag' => 0
            ])->all();
        $query2 = Rs_raised::find()->where([
            'user_id' => $user_id,
            'del_flag' => 0
        ])->all();
        $total = [];
        $total['project'] = $query;
        $total['raise'] = $query2;
        return $total;

    }

    public function getRaises($user_id){

        $query = Pr_support::find()
            ->where([
                'user_id' => $user_id, //user_id => user_id
                'del_flag' => 0
            ])->all();
        $inList = "";
        foreach($query as $key=>$id){
            $inList += $id['raised_id'];
            $inList += ",";
        }
        if($inList === ""){
            $inList += -1;
        }else{
            substr($inList, 0, -1);
        }
        $raised = Rs_raised::findBySql('SELECT * FROM rs_raised WHERE raised_id in ('. $inList.')')->all();
        return $raised;


    }

    public function getMyMsg($user_id){

        $query = Uc_msg::find()
            ->where([
                'to_user' => $user_id, //user_id => user_id
                'del_flag' => 0
            ])->orderBy('msg_id desc')
            ->all();
        return $query;

    }

    public function actionRegister(){

        $request = Yii::$app->request;
        //$user_name, $password, $phone, $email
        $user_name = $request->post("user_name");
        $password = $request->post("password");
        $_password = Yii::$app->getSecurity()->generatePasswordHash($password);
        $phone = $request->post("phone");
        $email = $request->post("email");
        //MD5加密  因为验证前台都做完了
        $user = new Uc_user();
        $user->user_name = $user_name;
        $user->password = $_password;
        $user->mobile = $phone;
        $user->email = $email;

        $session = Yii::$app->session;
        $session['user'] = $user;

        if($user->save() > 0){
            $this->redirect(['index/show']);
        }else {
            echo "<script>alert('系统错误请联系管理员')</script>";
        }
    }

    public function actionUpdateprofile(){

        $request = Yii::$app->request;
        //$user_name, $password, $phone, $email
        $user_name = $request->post("user_name");
        $password = $request->post("password");
        $_password = Yii::$app->getSecurity()->generatePasswordHash($password);
        $phone = $request->post("phone");
        $email = $request->post("email");
        //MD5加密  因为验证前台都做完了
        $user = new Uc_user();
        $user->user_name = $user_name;
        $user->password = $_password;
        $user->mobile = $phone;
        $user->email = $email;

        $session = Yii::$app->session;
        $session['user'] = $user;

        if($user->save() > 0){
            $this->redirect(['index/show']);
        }else {
            echo "<script>alert('系统错误请联系管理员')</script>";
        }
    }

    public function actionLogin(){

        $session = Yii::$app->session;
        if($session['user']['user_id'] != null){   //如果session存在直接跳到首页  总觉得这个不应该是我这边判断的 的确不是我这边判断的而是 filter来判断的
            $this->redirect(['index/show']);
        }
        $request = Yii::$app->request;
        $user_name = $request->post("user_name");
        $password = $request->post("password");
//        $_password = Yii::$app->getSecurity()->generatePasswordHash(password);

        $user = User::find()->where(['user_name'=>$user_name])->one();
        $hash = $user->password;
        $session['user_id'] = $user['user_id'];
        if(Yii::$app->getSecurity()->validatePassword($password, $hash)){
            $this->redirect(['index/show']);
        }else{
            echo "<script>alert('账号或者密码错误')</script>";
        }

    }

    public function actionValiuser($username){

        $usernameDb = User::find()->where(['user_name'=>$username])->one();
        if($usernameDb == null){
            return "1";
        }else{
            // echo "<script>alert('此用户名已经存在')</script>";
            return "0";
        }

    }

    public function actionSendmcap($mobile){
        //通过运营商发送短信验证码
    }



    public function actionValiphone($phone){
        $phoneDb = User::find()->where(['mobile'=>$phone])->one();
        if($phoneDb == null)
            echo "此手机号可以注册";
        else{
            echo "<script>alert('此手机号已经存在')</script>";
        }
    }

    public function actionValiemail($email){
        $emailDb = User::find()->where(['email'=>$email])->one();
        if($emailDb == null)
            echo "此email可以注册";
        else{
            echo "<script>alert('此email已经存在')</script>";
        }
    }

    public function logout(){
        if(!empty($_COOKIE['user_id'])){
            setcookie("user_id",time()-10);
            return $this->redirect(['index/show']);

        }else{
            echo "<script>alert('cookie 不存在')</script>";
            sleep(3);
            $this->redirect(['login/show']);
        }
    }




}